/*******************************************************************************
 * Dreampalace
 *******************************************************************************
 * 
 * Dreampalace is a special dreamdiary which helps dreamers to understand their
 * dreams. You can find also many features in Dreampalace to learn and hold the
 * lucid state in your dreams.
 * 
 * *****************************************************************************
 *
 * Program	   : Dreampalace
 * Autor           : PRo (Peter Rogge)
 * Email           : rogge-peter@gmx.de
 * Homepage        : http://code.google.com/p/dreampalace
 * License	   : GNU General Public License 3.0
 * 
 *******************************************************************************
 * 
 * This file is part of the project Dreampalace. Dreampalace is free software:
 * you can redistribute it and/or modify it under the terms of the GNU General 
 * Public License as published by the Free Software Foundation in version 3 of
 * the License only.
 * 
 * Dreampalace is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Dreampalace. If not, see <http://www.gnu.org/licenses/>.
 * 
 * The Dreampalace project designates this particular file as subject to the
 * "Classpath" exception as provided by the Dreampalace project in the GPL 
 * Version 3 section of the License file that accompanied this code.
 * 
 *******************************************************************************
 */
package de.pro.dreampalace.file.links;

import com.zenjava.jfxflow.actvity.AbstractActivity;
import com.zenjava.jfxflow.dialog.Dialog;
import com.zenjava.jfxflow.navigation.Place;
import de.pro.dreampalace.file.api.EFileTyp;
import de.pro.dreampalace.file.links.api.ILinksManager;
import java.util.ArrayList;
import java.util.Random;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import org.openide.util.Lookup;

/**
 * 
 * @author Peter Rogge (PRo) <rogge-peter@gmx.de>
 */
public final class LinksActivity extends AbstractActivity {
    
    @FXML private ScrollPane scrollPane = null;
    
    @Override
    protected final void activated() {
        System.out.println("LinksActivity.activated()");
        
        /*
         * TODO
         *  - Load links + categories from ILinksManager
         */
        
        ArrayList<String> categories = new ArrayList<String>();
        categories.add("Category 1");
        categories.add("Category 2");
        categories.add("Category 3");
        categories.add("Category 4");
        categories.add("Category 5");
        categories.add("Category 6");
        
        VBox vBox = new VBox(10);
        for (String category : categories) {
            vBox.getChildren().add(this.createCategoryPanel(category));
        }
        scrollPane.setContent(vBox);
    }
    
    private Node createCategoryPanel(String category) {
        StackPane sp = new StackPane();
        
        Rectangle re = new Rectangle();
//        re.setFill(new Color(0.3, 0.5, 0.7, 0.9));
        re.setFill(Color.RED);
        re.setStroke(Color.AQUAMARINE);
        sp.getChildren().add(re);
        
        // Categorytext
        DropShadow ds = new DropShadow();
        ds.setOffsetY(3.0);
        ds.setColor(Color.color(0.4, 0.4, 0.4));
        
        VBox vBox = new VBox();
        Text text = new Text();
        text.setCache(true);
        text.setEffect(ds);
        text.setText(category);
        text.setFont(Font.font(null, FontWeight.BOLD, 28));

        vBox.getChildren().add(text);
        
        // Links
        int random = (int)(Math.random() * 5) + 1;
        for (int j = 0; j < random; j++) {
            vBox.getChildren().add(this.createLinkPanel("Linkname " + j, null, null));
        }
        sp.getChildren().add(vBox);
        
        return sp;
    }
    
    private Node createLinkPanel(String link) {
        
        return null;
    }
    
    private VBox createLinkPanel(String link, String date, String description) {
        VBox vBox = new VBox();
        vBox.setPadding(new Insets(0, 15, 0, 15));
        
        Label lbl = new Label(link);
        lbl.setFont(new Font(20));
        vBox.getChildren().add(lbl);
        
        TextArea ta = new TextArea(
                "this is a description text which is very very long. "
                + "Really a very very long description. Really a very very long description. "
                + "Really a very very long description. "
                + "Really a very very long description. "
                + "Really a very very long description. "
                + "Really a very very long description. "
                + "Really a very very long description.");
        ta.setEditable(false);
        ta.setWrapText(true);
        ta.setPrefRowCount(3);
        vBox.getChildren().add(ta);
        
        lbl = new Label("Date: dd.MM.yyyy ss:mm | URL: https.googlecode.com/dreampalace/");
        lbl.setFont(new Font(12));
        vBox.getChildren().add(lbl);
        
        
        return vBox;
    }
    
    public final void onActionCreateNewCategory(final ActionEvent event) {
        System.out.println("LinksActivity.onActionCreateNewCategory(ActionEvent)");
        
        Dialog dialog = new Dialog("Create new category");
        dialog.setContent(new Label("You can put your own controls in it"));
        dialog.show(super.getView().toNode());
    }
    
    public final void onActionCreateNewLink(final ActionEvent event) {
        System.out.println("LinksActivity.onActionCreateNewLink(ActionEvent)");
        
        Dialog dialog = new Dialog("Create new link");
        dialog.setContent(new Label("You can put your own controls in it"));
        dialog.show(super.getView().toNode());
    }
    
    public final void onActionGoToPlaceLinksEditor(final ActionEvent event) {
        System.out.println("LinksActivity.onActionGoToPlaceLinksEditor(ActionEvent)");
        
        final ILinksManager lm = Lookup.getDefault().lookup(ILinksManager.class);
        lm.getNavigationManager().goTo(new Place(EFileTyp.LINKS_EDITOR.getPlace()));
    }
}
